<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class agama_model extends CI_model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function getChart($id_kec = 0, $id_kel = 0) {
        $filter = array();
        $filter['kode_kecamatan'] = 0;
        $filter['kode_kelurahan'] = 0;
        if ($id_kec != 0 && strlen($id_kec) > 0 && is_numeric($id_kec)) {
            $filter['kode_kecamatan'] = $id_kec;
        }
        if ($id_kel != 0 && strlen($id_kel) > 0 && is_numeric($id_kel)) {
            $filter['kode_kelurahan'] = $id_kel;
        }
        $query = '';
        if ($filter['kode_kecamatan'] == 0 && $filter['kode_kelurahan'] == 0) {
            $this->db->select('sum(jumlah_laki) as jumlah_laki, sum(jumlah_perempuan) as jumlah_perempuan, sum(jumlah) as jumlah, kode_agama as kategori_id, nama_agama as kategori');
            $this->db->group_by('kode_agama')
                    ->order_by('jumlah desc')->order_by('kode_agama');
            $query = $this->db->get('penduduk_per_agama');
        } else if ($filter['kode_kecamatan'] != 0 && $filter['kode_kelurahan'] != 0) {
            $this->db->select('sum(jumlah_laki) as jumlah_laki, sum(jumlah_perempuan) as jumlah_perempuan, sum(jumlah) as jumlah, kode_agama as kategori_id, nama_agama as kategori');
            $this->db->group_by('kode_agama')
                    ->order_by('jumlah desc')->order_by('kode_agama');
            $this->db->where($filter);
            $query = $this->db->get('penduduk_per_agama');
        } else if ($filter['kode_kecamatan'] != 0) {
            $this->db->select('sum(jumlah_laki) as jumlah_laki, sum(jumlah_perempuan) as jumlah_perempuan, sum(jumlah) as jumlah, kode_agama as kategori_id, nama_agama as kategori');
            $this->db->group_by('kode_agama')
                    ->order_by('jumlah desc')->order_by('kode_agama');
            $this->db->where(array('kode_kecamatan' => $filter['kode_kecamatan']));
            $query = $this->db->get('penduduk_per_agama');
        }
        //echo $this->db->last_query();
        return $query->result();
    }

    function get_jumlah_data_by_agama($kunci, $id_kec, $id_kel) {
        $sql = "select * from agama_master where no=$kunci";
        $query = $this->db->query($sql);
        $filter = array();
        foreach ($query->result() as $key => $row) {
            $filter[] = "agama=$kunci";
            break;
        }

        $sql = "select * from setup_kec where no_kec=$id_kec";
        $query = $this->db->query($sql);
        foreach ($query->result() as $key => $row) {
            $filter[] = "no_kec=$id_kec";
            break;
        }

        $sql = "select * from setup_kel where no_kel=$id_kel";
        $query = $this->db->query($sql);
        foreach ($query->result() as $key => $row) {
            $filter[] = "no_kel=$id_kel";
            break;
        }
        $s_filter = implode(' and ', $filter);
        if (strlen($s_filter)) {
            $s_filter = ' where ' . $s_filter;
        }
        $sql = "select count(*) as jumlah from biodata_wni_bgt  "
                . " $s_filter ";
        //echo $sql;
        $query = $this->db->query($sql);
        return $query->result()[0]->jumlah;
    }
    function get_data_penduduk_by_agama($kunci, $limit, $halaman, $id_kec, $id_kel){
        $sql = "select * from agama_master where no=$kunci";
        $query = $this->db->query($sql);
        $filter = array();
        foreach ($query->result() as $key => $row) {
            $filter[] = "agama=$kunci";
            break;
        }

        $sql = "select * from setup_kec where no_kec=$id_kec";
        $query = $this->db->query($sql);
        foreach ($query->result() as $key => $row) {
            $filter[] = "no_kec=$id_kec";
            break;
        }

        $sql = "select * from setup_kel where no_kel=$id_kel";
        $query = $this->db->query($sql);
        foreach ($query->result() as $key => $row) {
            $filter[] = "no_kel=$id_kel";
            break;
        }
        $s_filter = implode(' and ', $filter);
        if (strlen($s_filter)) {
            $s_filter = ' where ' . $s_filter;
        }
        $sql = "select *,agama as kategori_id from biodata_wni_bgt  "
                . " $s_filter "
                . "limit $halaman, $limit";
        //echo $sql;
        $query = $this->db->query($sql);
        return $query->result();
    }
    function get_list_agama(){
        $sql = "select no as kategori_id,descrip as kategori from agama_master order by no";
        $query = $this->db->query($sql);
        return $query->result();
        
    }
}
